/*

This do-file creates Figure 3

It uses "long" data from four waves:
2015, 2019, Apr 2020 and June 2020

And estimates:
- Income for lottery winners and losers, pre- and post-COVID (Panel A)
- Remittance and Non-Remittance Income Drops for lottery winners and losers (Panel B)
- Food Security for lottery winners and losers, pre- and post-COVID (Panel C)

*/


use "${data}/g2g_covid_income_long.dta", clear


local offset 0.25
local mid = 3.5 + `offset'
local barwidth = 1

gen time = 1 if inlist(_n, 1, 2)
replace time = 2 if inlist(_n, 3, 4)

gen group = 2 if _n==1
replace group = 1 if _n==2
replace group = 4.25 if _n==3
replace group = 3.25 if _n==4

gen b = .
gen se = .
gen byte migrant = 2 - mod(_n,2) if inrange(_n, 1, 4)

reg usd_win_inc if lottery==0 & post==0, cluster(union_Clean)

replace b = _b[_cons] if group==2
replace se = _se[_cons] if group==2

loc b_pre_cont = _b[_cons]
loc se_pre_cont = _se[_cons]


areg usd_win_inc lottery_post post lottery  i.round, ///
			absorb(upazila_Clean) cluster(union_Clean)
	
	
replace b = `b_pre_cont' + _b[lottery] if group==1
replace se = _se[lottery] if group==1

replace b = `b_pre_cont' + _b[post] if group==4.25
replace se = _se[post] if group==4.25	
	
replace b = `b_pre_cont' + _b[post] + _b[lottery] + _b[lottery_post] if group==3.25
replace se = _se[lottery_post] if group==3.25

local pre "Pre-COVID Surveys"
local post "Apr and Jun 2020"
local mig "Lottery Winners"
local nomig "Lottery Losers"
local ylab "Total Monthly Income (USD PPP)"



gen se_hi = b + 1.96*se
gen se_lo = b - 1.96*se


* Panel A: Diff-in-Diff Income
twoway  (bar b group if migrant==1 , fcolor(gs14) lcolor(gs0) barw(1)) ///
		(bar b group if migrant==2 , fcolor(red*0.9) lcolor(gs0) barw(1)) ///
		(rcap se_lo se_hi group, lcolor(gs0) msize(small) lwidth(thin)) ///
		(scatteri 0 2, color(none)), graphregion(color(white)) ///
		$grset xlabel(1.5 "`pre'" `mid' "`post'", notick) xtitle("") ///
		legend(order(2 "`mig'" 1 "`nomig'")) ytitle("`ylab'") plotregion(margin(sides))		

graph export "${figures}/Figure_3A.png", replace	


* For Remit / Non-Remit DiD, we want Diff income x lottery x remit/non-remit/non-remit
* Keep rounds with Decomposed Data
keep if inlist(round, 1, 3, 4)

foreach type in remit home {
	gen win_`type'_r2 = usd_win_inc_`type' if round==1
	egen win_`type'_pre = mean(win_`type'_r2), by(caseid)
	gen win_chg`type' = usd_win_inc_`type' - win_`type'_pre
}

keep if inlist(round, 3, 4)
keep caseid round lottery win_chg* upazila_Clean union_Clean

isid caseid round

reshape long win_chg, i(caseid round) j(type) str

gen remit = type=="remit"

gen lottery_remit = lottery * remit


local offset 0.25
local mid = 3.5 + `offset'
local barwidth = 1

gen time = 1 if inlist(_n, 1, 2)
replace time = 2 if inlist(_n, 3, 4)

gen group = 2 if _n==1
replace group = 1 if _n==2
replace group = 4.25 if _n==3
replace group = 3.25 if _n==4

gen b = .
gen se = .
gen byte migrant = 2 - mod(_n,2) if inrange(_n, 1, 4)

reg win_chg if remit==0 & lottery==0, cluster(union_Clean)

replace b = _b[_cons] if group==4.25
replace se = _se[_cons] if group==4.25

loc b_home_cont = _b[_cons]
loc se_home_cont = _se[_cons]


areg win_chg lottery remit lottery_remit lottery  i.round, ///
			absorb(upazila_Clean) cluster(union_Clean)
			
	
replace b = `b_home_cont' + _b[remit] if group==2
replace se = _se[remit] if group==2

replace b = `b_home_cont' + _b[lottery] if group==3.25
replace se = _se[lottery] if group==3.25	
	
replace b = `b_home_cont' + _b[remit] + _b[lottery] + _b[lottery_remit] if group==1
replace se = _se[lottery_remit] if group==1



local pre "Pre-COVID Surveys"
local post "Apr and Jun 2020"
local mig "Lottery Winners"
local nomig "Lottery Losers"
local ylab "Total Monthly Income (USD PPP)"


gen se_hi = b + 1.96*se
gen se_lo = b - 1.96*se

	local pre "Remittance Income"
	local post "Non-Remittance Income"
	local ylab "Change from 2018 to 2020"
	local mig "Lottery Winners"
	local nomig "Lottery Losers"

* Panel B: Diff-in-Diff Income Losss, by Source
twoway  (bar b group if migrant==1 , fcolor(gs14) lcolor(gs0) barw(1)) ///
		(bar b group if migrant==2 , fcolor(red*0.9) lcolor(gs0) barw(1)) ///
		(rcap se_lo se_hi group, lcolor(gs0) msize(small) lwidth(thin)) ///
		(scatteri 0 2, color(none)), graphregion(color(white)) ///
		$grset xlabel(1.5 "`pre'" `mid' "`post'", notick) xtitle("") ///
		legend(order(2 "`mig'" 1 "`nomig'")) ytitle("`ylab'") plotregion(margin(sides))		

graph export "${figures}/Figure_3B.png", replace	


use "${data}/g2g_covid_income_long.dta", clear

* Only the 1st and 4th rounds have food security data
keep if inlist(round, 1, 4)

local offset 0.25
local mid = 3.5 + `offset'
local barwidth = 1

gen time = 1 if inlist(_n, 1, 2)
replace time = 2 if inlist(_n, 3, 4)

gen group = 2 if _n==1
replace group = 1 if _n==2
replace group = 4.25 if _n==3
replace group = 3.25 if _n==4

gen b = .
gen se = .
gen byte migrant = 2 - mod(_n,2) if inrange(_n, 1, 4)

reg enough_food if lottery==0 & post==0, cluster(union_Clean)

replace b = _b[_cons] if group==2
replace se = _se[_cons] if group==2

loc b_pre_cont = _b[_cons]
loc se_pre_cont = _se[_cons]


areg enough_food lottery_post post lottery  i.round, ///
			absorb(upazila_Clean) cluster(union_Clean)
			
	
replace b = `b_pre_cont' + _b[lottery] if group==1
replace se = _se[lottery] if group==1

replace b = `b_pre_cont' + _b[post] if group==4.25
replace se = _se[post] if group==4.25	
	
replace b = `b_pre_cont' + _b[post] + _b[lottery] + _b[lottery_post] if group==3.25
replace se = _se[lottery_post] if group==3.25

local pre "2018"
local post "Jun 2020"
local mig "Lottery Winners"
local nomig "Lottery Losers"
local ylab "Food Security"



gen se_hi = b + 1.96*se
gen se_lo = b - 1.96*se

* Panel C: Diff-in-Diff Food Security
twoway  (bar b group if migrant==1 , fcolor(gs14) lcolor(gs0) barw(1)) ///
		(bar b group if migrant==2 , fcolor(red*0.9) lcolor(gs0) barw(1)) ///
		(rcap se_lo se_hi group, lcolor(gs0) msize(small) lwidth(thin)) ///
		(scatteri 0 2, color(none)), graphregion(color(white)) ///
		$grset xlabel(1.5 "`pre'" `mid' "`post'", notick) xtitle("") ///
		legend(order(2 "`mig'" 1 "`nomig'")) ytitle("`ylab'") plotregion(margin(sides))		

graph export "${figures}/Figure_3C.png", replace	


